package com.vogue.api.common.security.service;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;

import cn.hutool.core.util.StrUtil;
import com.vogue.api.common.redisson.RedissonService;
import com.vogue.api.common.security.model.Oauth2User;
import com.vogue.api.common.security.model.UserInfo;
import com.vogue.api.service.CateringUserService;
import lombok.SneakyThrows;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class Oauth2UserDetailsService {

    @Resource
    private CateringUserService cateringUserService;
    @Resource
    private RedissonService redissonService;

    private WxMaService service;

    /**
     * 手机号密码登录
     * @param mobile
     * @param password
     * @return
     */
    public Oauth2User loadUserByMobileAndPassword(String mobile, String password) {
        if (StrUtil.isEmpty(mobile) || StrUtil.isEmpty(password)) {
            throw new InvalidGrantException("您输入的手机号或密码不正确");
        }
        // 判断成功后返回用户细节
        Oauth2User oauth2User=cateringUserService.loadUserByMobileAndPassword(mobile,password);
        oauth2User.setAuthorities(AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_ADMIN,ROLE_USER,ROLE_API"));
        return oauth2User;
    }

    /**
     * 手机短信登录
     * @param mobile
     * @param smscode
     * @return
     */
    public Oauth2User loadUserByMobileAndSmsCode(String mobile, String smscode) {
        //校验数据规范
        if (StrUtil.isEmpty(mobile) || StrUtil.isEmpty(smscode)) {
            throw new InvalidGrantException("您输入的手机号或短信验证码不正确");
        }
        //校验验证码
        String code=redissonService.getSmsCode(mobile);
        if (!smscode.equals(code)){
            throw new InvalidGrantException("短信验证码过期或无效");
        }
        // 判断成功后返回用户细节
        Oauth2User oauth2User=cateringUserService.loadUserByMobileAndSmsCode(mobile);
        oauth2User.setAuthorities(AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_ADMIN,ROLE_USER,ROLE_API"));
        return oauth2User;
    }

    /**
     * 默认的方法
     * @param userAccount
     * @return
     * @throws UsernameNotFoundException
     */
    public Oauth2User findImUserByUserAccount(String userAccount){
        // 判断成功后返回用户细节
        Oauth2User oauth2User=cateringUserService.findImUserByUserAccount(userAccount);
        oauth2User.setAuthorities(AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_ADMIN,ROLE_USER,ROLE_API"));
        return oauth2User;
    }

    @SneakyThrows
    public Oauth2User miniProgramLogin(String code, UserInfo userInfo) {
        //获取微信openId和sessionKey
        WxMaJscode2SessionResult sessionResult= service.jsCode2SessionInfo(code);
        String openId=sessionResult.getOpenid();
        Oauth2User oauth2User=cateringUserService.findImUserByUserOpenId(openId,userInfo);
        oauth2User.setAuthorities(AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_ADMIN,ROLE_USER,ROLE_API"));
        return oauth2User;

    }

}
